package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * @author 韩先楚
 */
@RestController()
@RequestMapping("/admin/common")
@Api(tags = "后台管理-通用")
@Slf4j
public class CommonController {


    /**
     * 文件上传
     */
    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传 {}" ,file.getOriginalFilename());
        try {
            String filename = file.getOriginalFilename();
            String s = filename.substring(filename.lastIndexOf("."), filename.length());
            String s1 = UUID.randomUUID().toString()+ s;
            String url = "D:/image/" + s1;
            file.transferTo(new File(url));
            return Result.success(url);
        } catch (Exception e) {
           log.error("文件上传失败 {}", e.getMessage());
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
